@node Bitwise manipulation
@section Bitwise manipulation

@cindex binary data operation
@cindex bitwise integer operations
@cindex byte manipulation
Scheme48 provides two structures for bit manipulation: bitwise integer
operations, the @code{bitwise} structure, and homogeneous vectors of
bytes (integers between 0 and 255, inclusive), the @code{byte-vectors}
structure.

@subsection Bitwise integer operations

@stindex bitwise
The @code{bitwise} structure exports these procedures:

@deffn procedure bitwise-and integer @dots{} @returns{} integer
@deffnx procedure bitwise-ior integer @dots{} @returns{} integer
@deffnx procedure bitwise-xor integer @dots{} @returns{} integer
@deffnx procedure bitwise-not integer @returns{} integer
Basic twos-complement bitwise boolean logic operations.
@end deffn

@deffn procedure arithmetic-shift integer count @returns{} integer
Shifts @var{integer} by the given bit count.  If @var{count} is
positive, the shift is a left shift; otherwise, it is a right shift.
@code{Arithmetic-shift} preserves @var{integer}'s sign.
@end deffn

@deffn procedure bit-count integer @returns{} integer
Returns the number of bits that are set in @var{integer}.  If
@var{integer} is negative, it is flipped by the bitwise NOT operation
before counting.

@lisp
(bit-count #b11010010)                  @result{} 4@end lisp
@end deffn
 
@subsection Byte vectors

@stindex byte-vectors
The structure @code{byte-vectors} exports analogues of regular vector
procedures for @dfn{byte vectors}, homogeneous vectors of bytes:

@deffn procedure make-byte-vector length fill @returns{} byte-vector
@deffnx procedure byte-vector byte @dots{} @returns{} byte-vector
@deffnx procedure byte-vector? object @returns{} boolean
@deffnx procedure byte-vector-length byte-vector @returns{} integer
@deffnx procedure byte-vector-ref byte-vector index @returns{} byte
@deffnx procedure byte-vector-set! byte-vector index byte @returns{} unspecified
@var{Fill} and each @var{byte} must be bytes, @ie{} integers within the
inclusive range 0 to 255.  Note that @code{make-byte-vector} is not an
exact analogue of @code{make-vector}, because the @var{fill} parameter
is required.
@end deffn

Old versions of Scheme48 referred to byte vectors as `code vectors'
(since they were used to denote byte code).  The @code{code-vectors}
structure exports @code{make-code-vector}, @code{code-vector?},
@code{code-vector-length}, @code{code-vector-ref}, and
@code{code-vector-set!}, identical to the analogously named byte
vector operations.
